package com.heaven.service;

import com.heaven.mapper.UserMapper;
import com.heaven.pojo.User;
import com.heaven.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(User user) {
        String password = user.getPassword();
        String md = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md);
        User LoginUser = userMapper.login(user);
        if(LoginUser == null){
            return null;
        }
        String token = UUID.randomUUID().toString();
        return token;
    }
    @Override
    @Transactional
    public void addUser(User user) {
        Date date = new Date();
        String password = user.getPassword();
        String userPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(userPassword);
        user.setStatus(true).setUpdated(date).setCreated(date);
        userMapper.addUser(user);
    }

    @Override
    public PageResult userList(PageResult pageResult) {
        String query = pageResult.getQuery();
        Integer num = pageResult.getPageNum();
        Integer size = pageResult.getPageSize();
        Integer startNum = (num-1) * size;
        List<User> list = userMapper.userList(query,startNum,size);
        pageResult.setTotal(userMapper.userCount(pageResult.getQuery()));
        pageResult.setRows(list);
        return pageResult;
    }

    @Override
    @Transactional
    public void updateStatus(User user) {
        user.setUpdated(new Date());
        userMapper.userStatus(user);
    }

    @Override
    public User findUserId(Integer id) {
        User user = userMapper.findUserId(id);
        return user;
    }

    @Override
    @Transactional
    public Integer updateUser(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        String password = user.getPassword();
        String wornPassword = userMapper.selectPassword(user.getId());
        System.out.println(password);
        System.out.println(wornPassword);
        //System.out.println(password.equals(wornPassword));
        if(password.equals(wornPassword)){
            user.setUpdated(new Date());
            if (!StringUtils.hasLength(user.getNewpassword())){
                userMapper.updatePasswordUser(user);
            }else {
                user.setNewpassword(DigestUtils.md5DigestAsHex(user.getNewpassword().getBytes()));
                System.out.println(user.getUsername());
                userMapper.updateUser(user);
            }
            return 200;
        }
        return 201;
    }

    @Override
    public User auth(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        int i = userMapper.authUser(user);
        if (i==1){
            User verify = userMapper.auth(user);
            if (verify.getId()==1){
                return verify;
            }
        }
        return null;
    }

    @Override
    @Transactional
    public void deleteUser(Integer id) {
        System.out.println(id);
        userMapper.deleteUser(id);
    }
}
